﻿using System;
using System.Globalization;
using Dpx.Util;
using Xamarin.Forms;

namespace Dpx.Converters {
    /// <summary>
    /// 上次同步时间到字符串转换器。
    /// </summary>
    public class LastSyncToStringConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture) {
            if (!(value is DateTime time)) return null;
            var timeSpan = DateTime.Now - time;

            if (timeSpan.Days == 0) {
                return "今天刚刚成功地同步过。";
            }
            else if (timeSpan.Days > 100) {
                return "很久没有同步了。";
            }
            else {
                return $"距离上一次成功同步已有{timeSpan.Days}天。";
            }
        }

        public object ConvertBack(object value, Type targetType,
            object parameter, CultureInfo culture) {
            throw new DoNotCallThisException();
        }
    }
}